package com.aaa.ssm.service.impl;

import com.aaa.ssm.dao.FavoriteDao;
import com.aaa.ssm.entity.Favorite;
import com.aaa.ssm.service.FavoriteService;
import com.aaa.ssm.util.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Service
public class FavoriteServiceImpl implements FavoriteService {
    @Resource//依赖注入
    private FavoriteDao favoriteDao;
    @Resource
    HttpSession session;

    /**
     * 分页带参查询
     * @param page
     * @return
     */
    @Override
    public PageInfo queryPage(Page<Favorite> page) {
        PageHelper.startPage(page.getPageNo(),page.getPageSize());
        return new PageInfo(favoriteDao.queryByParm(page.getData()));
    }
    private static String time(){
        //获取当前时间
        Date date = new Date();
        //实例化格式化时间类
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //format格式化时间
        String format = simpleDateFormat.format(date);
        return format;
    }

    /**
     * 根据编号查询
     * @param id
     * @return
     */
    @Override
    public Favorite queryById(String id) {
        List<Favorite> favoriteList=favoriteDao.queryById(id);
        if (favoriteList !=null && favoriteList.size()>0){
            return favoriteList.get(0);
        }
        return null;
    }

    /**
     * 添加数据
     * @param favorite
     * @return
     */
    @Override
    public int add(Favorite favorite) {
        String attribute =(String) session.getAttribute("nameA");
        favorite.setFavrid(attribute);
        //将时间赋值
        favorite.setFavtime(time());
        return favoriteDao.add(favorite);
    }

    /**
     * 更新
     * @param favorite
     * @return
     */
    @Override
    public int update(Favorite favorite) {
        String attribute =(String) session.getAttribute("nameA");
        favorite.setFavrid(attribute);
        //将时间赋值
        favorite.setFavtime(time());
        return favoriteDao.update(favorite);
    }

    /**
     * 根据编号删除
     * @param id
     * @return
     */
    @Override
    public int deleteById(String id) {
        return favoriteDao.deleteById(id);
    }
}
